/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycity.servlets;

import com.mycity.dao.CommentTab;
import com.mycity.dao.PostTab;
import com.mycity.dao.UserTab;
import com.mycity.jsonops.PostOps;
import com.mycity.jsonops.UserOps;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author celikeins
 */
public class User extends Base {

    public User(HttpServletRequest request, HttpServletResponse response) {
        super(request, response);
    }

    public void loginAction() throws ServletException, IOException {
        String email = request.getParameter("username");
        String password = request.getParameter("password");
        if (email == null || email.isEmpty() || password == null || password.isEmpty()) {
            response.sendRedirect("/MyCityWeb/login.jsp");
            return;
        }
        UserTab user = UserOps.isCredentialsValid(email, password);
        if (user != null ) {
            HttpSession session = request.getSession();
            session.setAttribute("user_info", user);
            response.sendRedirect("/MyCityWeb/home.jsp");
        } else {

            response.sendRedirect("/MyCityWeb/login.jsp?loginFailed=1");
        }
    }

    public void registerAction() throws Exception {
        String email = request.getParameter("email");
        String name = request.getParameter("name");
        String surname = request.getParameter("sname");
        String password = request.getParameter("password1");
        String username = request.getParameter("username");
        if (email == null) {
            response.sendRedirect("/MyCityWeb/register.jsp?registerFailed=1");
            return;
        }
        if (!password.equals(request.getParameter("password2"))) {
            response.sendRedirect("/MyCityWeb/register.jsp?registerFailed=1");
            return;
        }
        Boolean emailAvailable=UserOps.checkEmail(email),
                usernameAvailable=UserOps.checkUsername(username);
        if(emailAvailable==null || !emailAvailable || usernameAvailable==null || !usernameAvailable){
            response.sendRedirect("/MyCityWeb/register.jsp?registerFailed=1");
            return;
        }
        UserTab user = new UserTab(null, email, username, name, surname, password, null, null,null,null);

        user = UserOps.createUser(user);
        if (user != null) {
            HttpSession session = request.getSession();
            session.setAttribute("user_info", user);
            response.sendRedirect("/MyCityWeb/Profile/index");
        } else {
            response.sendRedirect("/MyCityWeb/register.jsp?registerFailed=1");
            System.err.println("Error during user creation");
            //TODO error
        }
    }

    public void addBio() throws IOException {
        UserTab user = getCurrentUser();
        if (user == null) {
            response.sendRedirect("/MyCityWeb/login.jsp");
            return;
        }
        String newBio = request.getParameter("info");
        Boolean success = null;
        if (newBio != null) {
            success = UserOps.updateBio(user.getUserId(), newBio);
        }

        if (success != null && success) {
            UserTab newUser = UserOps.getUserById(user.getUserId());
            setCurrentUser(newUser);
        }
        response.sendRedirect("/MyCityWeb/User/profile/"+user.getUserId());

    }

    public void changeProfilePic() throws IOException {
        UserTab user = getCurrentUser();
        if (user == null) {
            response.sendRedirect("/MyCityWeb/login.jsp");
            return;
        }
        String id = request.getParameter("photo_id");
        Boolean success = null;
        if (id != null) {
            success = UserOps.changeProfilePic(user.getUserId(), id);
        }

        if (success != null && success) {
            UserTab newUser = UserOps.getUserById(user.getUserId());
            setCurrentUser(newUser);
        }
        response.sendRedirect("/MyCityWeb/User/profile/"+user.getUserId());


    }
    
    public void profile(Long userId) throws IOException, ServletException{
  
        response.setContentType("text/html");
        UserTab owner = UserOps.getUserById(userId);
        PostTab[] posts = PostOps.getPostsByUserId(userId);

        request.setAttribute("userposts", posts);
        request.setAttribute("owner", owner);
        request.getRequestDispatcher("/publicProfile.jsp").include(request, response);
    
    }

    public void logout() throws ServletException, IOException {
        request.getSession().removeAttribute("user_info");
        response.sendRedirect("/MyCityWeb/login.jsp");
    }
}
